package com.soft.mainsite.module.data.service.impl;

import com.soft.mainsite.common.domain.RestResponse;
import com.soft.mainsite.module.data.dao.UserDataMapper;
import com.soft.mainsite.module.data.dto.FavoriteDTO;
import com.soft.mainsite.module.data.dto.UserDataDTO;
import com.soft.mainsite.module.data.dto.UserDataDeleteDTO;
import com.soft.mainsite.module.data.entity.UserData;
import com.soft.mainsite.module.data.service.DataService;
import com.soft.mainsite.module.data.service.UserDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2020/11/28 23:25
 * Stay hungry,stay foolish!
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UserDataServiceImpl implements UserDataService {

    @Resource
    private UserDataMapper userDataMapper;

    @Resource
    private DataService dataService;

    @Override
    public RestResponse saveUserData(UserData userData) {

        userData.setCreateTime(new Date());
        userData.setIsReceived(1);
        userData.setIsFavorite(0);
        userData.setUserState(1);
        userData.setStatus(0);
        int insert = userDataMapper.saveUserData(userData);
        if (insert > 0) {
            log.info("E|UserDataServiceImpl|saveUserData()|用户收藏文件资料成功！");
            return RestResponse.success(userData);
        }

        log.error("E|UserDataServiceImpl|saveUserData()|用户收藏文件资料失败！");

        return RestResponse.success("用户收藏文件资料失败！", -1);
    }

    @Override
    public RestResponse delete(UserDataDeleteDTO userDataDeleteDTO) {

        String userPhone = userDataDeleteDTO.getUserPhone();
        Integer[] userDataIds = userDataDeleteDTO.getUserDataIds();

        if (userDataIds != null && userDataIds.length > 0) {

            for (Integer userDataId : userDataIds) {
                int delete = userDataMapper.deleteByUserPhoneAndDataId(userPhone, userDataId);
                if (delete > 0) {
                    log.info("E|UserDataServiceImpl|delete()|用户删除文件资料成功！被删除的资料id = {}, 用户手机号 = {}", userDataId, userPhone);
                }
            }
        }

        return RestResponse.success();
    }

    @Override
    public RestResponse userDataList(FavoriteDTO favoriteDTO) {

        // 先根据当前手机号去库里查询是否已经存在
        // 1、不存在，向库中插入默认的几条数据
        // 2、已经存在，直接返回

        String userPhone = favoriteDTO.getUserPhone();
        Integer isFavorite = favoriteDTO.getIsFavorite();
        List<UserDataDTO> userDataDTOList = userDataMapper.userDataList(userPhone, isFavorite);

        return RestResponse.success(0, "成功！", userDataDTOList);
    }

    @Override
    public RestResponse addFavorite(FavoriteDTO favoriteDTO) {

        Integer dtoId = favoriteDTO.getId();

        int add = userDataMapper.addFavorite(dtoId);
        if (add > 0) {
            log.info("E|UserDataServiceImpl|updateUserData()|用户添加收藏文件资料成功！");
            return RestResponse.success();
        }

        log.error("E|UserDataServiceImpl|updateUserData()|用户添加收藏文件资料失败！");

        return RestResponse.success("用户添加收藏文件资料失败！", -1);
    }

    @Override
    public RestResponse cancelFavorite(FavoriteDTO favoriteDTO) {

        Integer dtoId = favoriteDTO.getId();
        int cancel = userDataMapper.cancelFavorite(dtoId);
        if (cancel > 0) {
            log.info("E|UserDataServiceImpl|updateUserData()|用户取消收藏文件资料成功！");
            return RestResponse.success();
        }

        log.error("E|UserDataServiceImpl|updateUserData()|用户取消收藏文件资料失败！");

        return RestResponse.success("用户取消收藏文件资料失败！", -1);
    }
}
